|
|
|
|
|
|
|
In the case of a user clicking an Exit button, you need to add code to the Exit button's Click event procedure. Let's illustrate this by writing code for the Click event procedure of your Loan Calculator program's Exit button. |
|
|
|
|
|
|
|
|
Double-click the Exit button that you placed on the sample application's form. You'll see a new window called a Code window (see Figure 2.17). You can open a separate code window for each form (or other kind of module) in your project, and this is where you place code that relates to that form and the objects contained in it. Notice that the Code window contains a template, or shell, of a sub procedure, beginning with the words Private Sub and ending with the words End Sub. A sub procedure (also known simply as a procedure) is a discrete sequence of code statements that has a name and is executed as a unit. |
|
|
|
|
|
|
|
|
Figure 2.17
The Code window is a full-featured editor for
the code that relates to your program's objects. |
|
|
|
|
|
|
|
|
The part after the words Private Sub denotes the sub procedure's name. This particular sub procedure is named cmdExit_Click, a predefined name that denotes the Click event procedure for the control named cmdExit. Visual Basic will execute any code located within this sub procedure whenever the Click event occurs to this command button. |
|
|
|
|
|
|
|
|
|
Most controls can react to one of several different events; each type of control has a default event, which is the event that usually occurs to that type of control most often. For example, the Click event is the one that occurs most often to a CommandButton control. The default event is the one that is opened in the Code window when you double-click a new control.  |
|
|
|
|
|
|
|
|
|
|
To cause the program to end when the user clicks the Exit button, you simply need to add one line of codeVisual Basic's End statementto the cmdExit_Click procedure. Your cursor should already be on the blank line between the Private Sub cmdExit_Click() and End Sub statements; if it's not, simply click there. Press Tab to indent the code (that makes it easier to read), and then type the word End. Press Enter to insert a new blank line into the procedure (this isn't necessary, but the extra blank line improves readability). When you're done, your complete sub procedure should look like this code: |
|
|
|
|
|
|
|
|
Private Sub cmdExit_Click()
End
End Sub |
|
|
|
|
|